<?php
//$Id: cookie.php 129 2011-12-30 10:10:13Z tomqin@gmail.com $

/**
 * Cookie 配置文件
 *
 * @copyright		Copyright (C) 2010-2012 ARESCMS Inc.
 * @author			TomQin <tomqin@gmail.com>
 * @license			http://www.arescms.cn/license/
 * @package Core
 */

final Class Cookie
{
	/**
	 * Determine if a cookie exists.
	 *
	 * @param string
	 * @return boolean
	 */
	public static function has($name)
	{
		return isset($_COOKIE[$name]);
	}

	/**
	 * Get the value of a cookie.
	 *
	 * @param string
	 * @param mixed
	 * @return string
	 */
	public static function get($name, $default = null)
	{
		if (isset($_COOKIE[$name]) && $_COOKIE[$name])
		{
			// All Laravel managed cookies are "signed" with a fingerprint hash.
			// The hash serves to verify that the contents of the cookie have not
			// been modified by the user. We can verify the integrity of the cookie
			// by extracting the value and re-hashing it, then comparing that hash
			// against the hash stored in the cookie.
			if (isset($value[40]) and $value[40] === '~')
			{
				list($hash, $value) = explode('~', $value, 2);

				if (self::hash($name, $value) === $hash)
				{
					return $value;
				}
			}
		}
		else
		{
			return $default;
		}
	}

	/**
	 * Set a "permanent" cookie. The cookie will last for one year.
	 *
	 * @param  string
	 * @param  string
	 * @param  string
	 * @param  string
	 * @param  boolean
	 * @param  boolean
	 * @return boolean
	 */
	public static function forever($name, $value, $path = '/', $domain = null, $secure = false, $http_only = false)
	{
		return self::put($name, $value, 525600, $path, $domain, $secure, $http_only);
	}

	/**
	 * Set the value of a cookie.
	 *
	 * If a negative number of minutes is specified, the cookie will be deleted.
	 *
	 * This method's signature is very similar to the PHP setcookie method.
	 * However, you simply need to pass the number of minutes for which you
	 * wish the cookie to be valid. No funky time calculation is required.
	 *
	 * @param string	The name of the cookie.
	 * @param string	The value of the cookie.
	 * @param integer	The minutes the cookie expires after current time.
	 * @param string	The path on the server in which the cookie will be available on.
	 * @param string	The domain that the cookie is available to.
	 * @param boolean	Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client.
	 * @param boolean	When TRUE the cookie will be made accessible only through the HTTP protocol.
	 * @return boolean
	 */
	public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false, $http_only = false)
	{
		if (headers_sent()) return false;

		$time = ($minutes !== 0) ? time() + ($minutes * 60) : 0;

		$value = self::hash($name, $value).'~'.$value;

		if ($minutes < 0)
		{
			unset($_COOKIE[$name]);
		}
		else
		{
			$_COOKIE[$name] = $value;
		}

		return setcookie($name, $value, $time, $path, $domain, $secure, $http_only);
	}

	/**
	 * Generate a cookie hash.
	 *
	 * Cookie salts are used to verify that the contents of the cookie have not
	 * been modified by the user, since they serve as a fingerprint of the cookie
	 * contents. The application key is used to salt the salts.
	 *
	 * When the cookie is read using the "get" method, the value will be extracted
	 * from the cookie and hashed, if the hash in the cookie and the hashed value
	 * do not match, we know the cookie has been changed on the client.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @return string
	 */
	protected static function hash($name, $value)
	{
		return sha1($name.$value.Config::get('application.key'));
	}

	/**
	 * Delete a cookie.
	 *
	 * @param  string  $name
	 * @return boolean
	 */
	public static function forget($name)
	{
		return self::put($name, null, -2000);
	}
}
?>